
#import "RIBMakeCubeFaceEnvironment.h"
#import "ri/ri.h"

@implementation RIBMakeCubeFaceEnvironment

+ initialize { return [RIBMakeCubeFaceEnvironment setVersion:1], self; }

- init
{
  [super init];

  pX = nX = pY = nY = pZ = nZ = textureName = NULL;
  filterFunc = (RtFloatFunc)RiGaussianFilter;
  filterFuncName = NXCopyStringBuffer("gaussian");
  sWidth = tWidth = 3.0;

  autoGenerate = NO;
  onlyGenerateForFirstFrameWhenShooting = NO;
  epsilonForAutoGenerate = 1./31.;

  myRect.origin.x = 0.0; 
  myRect.origin.y = 0.0; 
  myRect.size.width  = 64.0; 
  myRect.size.height = 64.0; 

  autoScale = YES;
  autoScalePercentage = .25;

  return self;
}

- setPX:(const char *)newPX nX:(const char *)newNX
     pY:(const char *)newPY nY:(const char *)newNY
     pZ:(const char *)newPZ nZ:(const char *)newNZ
     textureName:(const char *)newTextureName
     filterFuncName:(const char *)newFilterFuncName
     fov:(RtFloat)newFOV
     sWidth:(RtFloat)newSWidth tWidth:(RtFloat)newTWidth 
     n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
     printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
{
   pX = NXCopyStringBuffer(newPX);
   nX = NXCopyStringBuffer(newNX);
   pY = NXCopyStringBuffer(newPY);
   nY = NXCopyStringBuffer(newNY);
   pZ = NXCopyStringBuffer(newPZ);
   nZ = NXCopyStringBuffer(newNZ);
   textureName = NXCopyStringBuffer(newTextureName);
   if (!strcmp(newFilterFuncName, "box"))
   {  filterFunc = (RtFloatFunc)RiBoxFilter; 
      filterFuncName = NXCopyStringBuffer("box"); 
   }
   if (!strcmp(newFilterFuncName, "triangle"))
   {  filterFunc = (RtFloatFunc)RiTriangleFilter; 
      filterFuncName = NXCopyStringBuffer("triangle"); 
   }
   if (!strcmp(newFilterFuncName, "catmull-rom"))
   {  filterFunc = (RtFloatFunc)RiCatmullRomFilter; 
      filterFuncName = NXCopyStringBuffer("catmull-rom"); 
   }
   if (!strcmp(newFilterFuncName, "sinc"))
   {  filterFunc = (RtFloatFunc)RiSincFilter; 
      filterFuncName = NXCopyStringBuffer("sinc"); 
   }
   if (!strcmp(newFilterFuncName, "gaussian"))
   {  filterFunc = (RtFloatFunc)RiGaussianFilter; 
      filterFuncName = NXCopyStringBuffer("gaussian"); 
   }

   fov = newFOV;
   sWidth = newSWidth;
   tWidth = newTWidth;

   [self setN:newN tokens:newTokens parms:newParms archiveVector:newArchiveVector
             printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];

   // okay, now we have to check our parameter list to see if any of my other flags have been set...

   return self;
}

- setAutoGenerate:(BOOL)flag
{
  if (flag) // okay, we're setting it True
  {  // if it wasn't already true, malloc up a tmpCamera and set it true
     if (!autoGenerate)
     {  tmpCamera = [[WW3DCamera alloc] initFrame:&myRect];
     } 
     autoGenerate = YES;
  }  
  else // we're turning it off
  {  // if it wasn't already true, malloc up a tmpCamera and set it true
     if (autoGenerate)
     {  [tmpCamera setWorldShape:nil]; // just to be sure it doesn't do anything stupid...
        [tmpCamera free];
     } 
     autoGenerate = NO;
  }  
  return self;
}
 
- renderMaps:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime usingStream:(NXStream *)ns
{
   if (autoGenerate)
   {  // okay, we now want to set the worldShape of our camera 
      [tmpCamera setWorldShape:[camera worldShape]];

      // now we need to figure out where the heck we are and put the camera there.
      // the way we do that is we grab our shape, get it's inverse transform from the camera to it
      // we do that by getting the my shap
      // we then take that 4x4 and transform it by the ribCommand list members until we hit here.

   }
   NXPrintf(ns, 
	    "MakeCubeFaceEnvironment \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" %f \"%s\" %f %f ", 
	    pX, nX, pY, nY, pZ, nZ, textureName, fov, filterFuncName, sWidth, tWidth);
   [super writeParameterList:ns];
   return self;
}

- renderMaps:(WW3DCamera *)camera usingStream:(NXStream *)ns
{
  RtFloat  shutterOpenTime = [camera shutterOpenTime],
           shutterCloseTime = [camera shutterCloseTime];


  return [self renderMaps:camera startingAt:shutterOpenTime endingAt:shutterCloseTime usingStream:ns];
}


- renderMaps:(WW3DCamera *)camera startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiMakeCubeFaceEnvironmentV(pX, nX, pY, nY, pZ, nZ, textureName, fov, filterFunc, sWidth, tWidth, n, tokens, parms);

  return self;
}

- renderMaps:(WW3DCamera *)camera
{
  RtFloat  shutterOpenTime = [camera shutterOpenTime],
           shutterCloseTime = [camera shutterCloseTime];


  return [self renderSelf:camera startingAt:shutterOpenTime endingAt:shutterCloseTime];
}


- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, 
	    "MakeCubeFaceEnvironment {%s} {%s} {%s} {%s} {%s} {%s} {%s} %f {%s} %f %f ", 
	    pX, nX, pY, nY, pZ, nZ, textureName, fov, filterFuncName, sWidth, tWidth);
   [super writeParameterList:stream];
   return self;
}

#define typeVector "*******f*ffccfffcf"
#define typeValues &pX, &nX, &pY, &nY, &pZ, &nZ, &textureName, \
        &fov, &filterFuncName, &sWidth, &tWidth, \
        &autoGenerate, &onlyGenerateForFirstFrameWhenShooting, \
        &epsilonForAutoGenerate, &myRect.size.width, &myRect.size.height, \
        &autoScale, &autoScalePercentage

- read:(NXTypedStream *)stream 
{
    int version;
    [super read:stream];

    version = NXTypedStreamClassVersion(stream, "RIBMakeCubeFaceEnvironment");
    if (version == 0) NXReadTypes(stream, "i", &version), version=1;
    if (version == 1)
    {  NXReadTypes(stream, typeVector, typeValues);
    } 
    else 
    {
    }
    return self;
}

- write:(NXTypedStream *)stream 
{
    [super write:stream];

    NXWriteTypes(stream, typeVector, typeValues);

    return self;
}

@end
